#!/usr/bin/env bash
# Package the ecCodes WMO-13 overlay into a versioned tarball with checksum.
#
# Usage:
#   ./scripts/package_eccodes_overlay.sh [output-dir]
#
# The script creates <output-dir>/eccodes_wmo13_overlay.tar.gz (default: deps)
# and writes a matching SHA-256 digest alongside the archive.

set -euo pipefail

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
ROOT_DIR="$(cd "$SCRIPT_DIR/.." && pwd)"
OVERLAY_DIR="$ROOT_DIR/deps/eccodes_overrides"
OUTPUT_DIR="${1:-$OVERLAY_DIR}"

if [ ! -d "$OVERLAY_DIR/bufr" ]; then
    echo "ERROR: Overlay directory not found at $OVERLAY_DIR" >&2
    exit 1
fi

mkdir -p "$OUTPUT_DIR"
OUTPUT_DIR="$(cd "$OUTPUT_DIR" && pwd)"
ARCHIVE="$OUTPUT_DIR/eccodes_wmo13_overlay.tar.gz"

pushd "$OVERLAY_DIR" >/dev/null

tar -czf "$ARCHIVE" bufr/ README.md checksums.sha256
sha256sum "$ARCHIVE" > "$ARCHIVE.sha256"

popd >/dev/null

echo "Created overlay archive: $ARCHIVE"
echo "Checksum written to: $ARCHIVE.sha256"
